﻿package com.PlantVsZombies.Zombies;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;

import com.PlantVsZombies.Bullet.Bullet;
import com.PlantVsZombies.common.Life;
import com.PlantVsZombies.ui.GamePanel2;
import com.PlantVsZombies.util.ImageUtil;
/**
 * 逗比模式 僵尸
 * @author wangdongdong
 *
 */
public class Zombie_Enemy extends Life {

	private int life;
	private int initlife;
	private ImageIcon image;
	private int x;
	private int y;
	private int speed;
	private boolean canRun = true;
	private boolean isLive = true;
	private boolean isAttack = false;
	private int shootIndex = 0;
	private boolean isboss = false;
	private int type;
	int move = 1;
	int move0 = 1;
	int move1 = 1;
	private ArrayList<Bullet> bullet = new ArrayList<Bullet>();
	private ArrayList<Star> star = new ArrayList<Star>();

	public int getLife() {
		return life;
	}

	public void setLife(int life) {
		this.life = life;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}


	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public boolean isCanRun() {
		return canRun;
	}

	public void setCanRun(boolean canRun) {
		this.canRun = canRun;
	}

	public boolean getLive() {
		return isLive;
	}

	public void setLive(boolean isLive) {
		this.isLive = isLive;
	}

	public boolean isAttack() {
		return isAttack;
	}

	public void setAttack(boolean isAttack) {
		this.isAttack = isAttack;
	}

	public void setImage(ImageIcon image) {
		this.image = image;
	}

	public ImageIcon getImage() {
		return image;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public boolean isIsboss() {
		return isboss;
	}

	public void setIsboss(boolean isboss) {
		this.isboss = isboss;
	}

	public ArrayList<Bullet> getBullet() {
		return bullet;
	}

	public void setBullet(ArrayList<Bullet> bullet) {
		this.bullet = bullet;
	}

	Random rd = new Random();

	public Zombie_Enemy(int x, int y, int speed) {
		this.x = x;
		this.y = y;
		this.speed = speed;
		this.image = getImage(1);
		this.life = 4;
		this.initlife = 200;
	}

	public Zombie_Enemy(int x, int y, int speed, ImageIcon image,int life) {
		this.x = x;
		this.y = y;
		this.speed = speed;
		this.image = image;
		this.life =life;
		this.initlife = life;
		isboss = true;
		this.type = -1;
	}

	public void move() {

		if (isboss) {
			this.y -= move;
			if (this.y < 20) {
				move = -move;
			}
			if (this.y > 400)
				move = -move;
		} else if (type == 0 || type == 3) {
			this.x--;
		} else if (type == 1 || type == 2) {
			this.x -= 1;
			this.y -= move0;
			if (this.y < 20) {
				move0 = -move0;
			}
			if (this.y > 450)
				move0 = -move0;
		}

	}

	public void shootBullet() {
		if (isboss) {
			for (int i = 0; i < 3; i++) {
				Bullet bt = new Bullet(x, y +50, ImageUtil.bossbullet1.getImage(),
						i+3);
				bt.start();
				bullet.add(bt);
			}
		} else {
			Bullet bt = new Bullet(x, y, ImageUtil.zombiebullet1, 0);
			bt.start();
			bullet.add(bt);
			
		}
	}

	public void drawMe(Graphics g) {
		if (isboss)
			drawLife(g);
		drawBullet(g);
		if (type == -1)
			g.drawImage(this.image.getImage(), x, y, image.getIconWidth() - 50,
					image.getIconHeight() - 50, null);
		else if(type == 0){
			g.drawImage(image.getImage(), x, y, image.getIconWidth() - 50,
					image.getIconHeight() - 50, null);
		}
		else if (type == 1)
			g.drawImage(image.getImage(), x, y, null);
		else if (type == 2)
			g.drawImage(image.getImage(), x, y, null);
		else if (type == 3)
			g.drawImage(image.getImage(), x, y, null);

	}

	// 画血条
	public void drawLife(Graphics g) {
		g.setColor(Color.GRAY);
		g.fillRect(x - 4, y - 24, 156, 23);
		g.setColor(Color.RED);
		g.fillRect(x, y - 20, 150 * life / initlife, 15);
	}

	// 画子弹
	public void drawBullet(Graphics g) {
		for (int i = 0; i < bullet.size(); i++) {
			Bullet bt = bullet.get(i);
			if (bt.isLive()) {
				if(isboss)
					bt.drawMe(g);
				else
					bt.drawMe(g,20,10);
			} else
				bullet.remove(bt);
		}
	}

	public ImageIcon getImage(int x) {

		int num = rd.nextInt(10);
		if (num % 2 == 0) {
			type=0;
		} else if (num == 3 || num == 9) {
			type=1;
		} else if (num == 1 || num == 5) {
			type=2;
		}else{
			type = 3;
		}
		return ImageUtil.zombies[type];
	}

	public Star produceStar() {
		Star s = null;
		int num = rd.nextInt(10);
		if (num % 2 == 0) {
			s = new Star(x, y, 0);
		} else if (num % 3 == 0) {
			s = new Star(x, y, 1);
		}else if(num == 1){
			s = new Star(x, y, 2);
		}
		s.start();
		return s;
	}

	public void getScore() {
		switch (this.type) {
		case 0:
			GamePanel2.score += 50;
			break;
		case 1:
			GamePanel2.score += 100;
			break;
		case 2:
			GamePanel2.score += 100;
			break;
		case 3:
			GamePanel2.score += 200;
			break;
		case -1:
			GamePanel2.score += 1000;
			break;
		}
	}

	@Override
	public int doRun() {
		this.move();

		if (shootIndex++ % 100 == 0) {

			this.shootBullet();
			this.move0 = rd.nextInt(2) - 2;
		}
		try {
			Thread.sleep(15);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (this.life <= 0) {
			Star s;
			if ((s = produceStar()) != null)
				GamePanel2.star.add(s);
			this.isLive = false;
			getScore();
			return 0;
		}
		return 1;
	}
}
